package com.zgc.view.controller;

import com.fx.spring.annotation.ControllerFX;
import com.fx.spring.controller.AbstractController;
import com.jfoenix.controls.JFXAlert;
import com.jfoenix.controls.JFXProgressBar;
import com.zgc.zkinfo.ZkService;
import javafx.collections.ObservableList;
import javafx.concurrent.Worker;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.net.URL;
import java.util.ResourceBundle;

@ControllerFX(fxmlURL = "/view/fxml/ProgressDialog.fxml")
public class ProgressAlertCtrl extends AbstractController<VBox> {

    @FXML
    protected VBox alertDialog;
    @FXML
    private Label alertMessage;
    @FXML
    private JFXProgressBar alertProgressBar;

    private JFXAlert<String> alert;
    private ObservableList<String> styleClass;

    @FXML
    @Override
    public void initialize(URL location, ResourceBundle resources) {
        logger.debug("[{}]初始化完成", location);
        alert = initAlert();
        alertProgressBar.setPrefWidth(500);
        styleClass = this.alertProgressBar.getStyleClass();

    }


    public void showProgressAlert(String title, ZkService zkService) {
        // 先加载在设置
        VBox load = this.load();
        alert.setContent(load);
        alert.setTitle(title);
        alert.show();
        alert.setOnCloseRequest(event -> {
            setDefaultProgressColor();
            if (!Worker.State.SUCCEEDED.equals(zkService.getState())) {
                zkService.cancel();
            }
        });
    }


    /**
     * 获取弹窗message
     *
     * @return {@link Label}
     */
    public Label getAlertMessage() {
        return this.alertMessage;
    }

    /**
     * 获取弹窗进度条
     *
     * @return {@link JFXProgressBar}
     */
    public JFXProgressBar getAlertProgress() {
        return this.alertProgressBar;
    }

    /**
     * 设置弹窗进度条
     */
    public void setAlertProgress(double value) {
        this.alertProgressBar.setProgress(value);
    }

    public void setDefaultProgressColor() {
        this.alertProgressBar.setProgress(-1);
        removeProgressColor();
        this.alertProgressBar.getStyleClass().addAll("dialog-default-progress-bar");
    }

    public void setSuccessProgressColor() {
        this.alertProgressBar.setProgress(1);
        removeProgressColor();
        styleClass.add("dialog-success-progress-bar");
    }

    public void setFailProgressColor() {
        this.alertProgressBar.setProgress(1);
        removeProgressColor();
        styleClass.add("dialog-fail-progress-bar");
    }


    private void removeProgressColor() {
        styleClass.remove("dialog-default-progress-bar");
        styleClass.remove("dialog-success-progress-bar");
        styleClass.remove("dialog-fail-progress-bar");
    }


    private JFXAlert<String> initAlert() {
        JFXAlert<String> alert = new JFXAlert<>();
        Stage window = (Stage) alert.getDialogPane().getScene().getWindow();
        window.getIcons().add(new Image("/view/images/icon.png"));
        return alert;
    }
}
